<%@ page language="java" import="weaver.conn.RecordSet" pageEncoding="UTF-8" %>
<%@ page import="weaver.general.Util" %>
<%@ page import="com.alibaba.fastjson.JSONObject" %>
<%@ page import="weaver.general.BaseBean" %>
<%@ page import="org.apache.http.impl.client.CloseableHttpClient" %>
<%@ page import="org.apache.http.impl.client.HttpClients" %>
<%@ page import="org.apache.http.client.methods.HttpGet" %>
<%@ page import="org.apache.http.client.config.RequestConfig" %>
<%@ page import="org.apache.http.client.methods.CloseableHttpResponse" %>
<%@ page import="org.apache.http.HttpStatus" %>
<%@ page import="org.apache.http.HttpEntity" %>
<%@ page import="org.apache.http.util.EntityUtils" %>
<%@ page import="java.io.IOException" %>
<%@ page import="java.util.Map" %>
<%@ page import="java.util.HashMap" %>
<%
    JSONObject data = new JSONObject();
    String keyword = Util.null2String( request.getParameter( "keyword" ) );
    String token = "3c6b514d-2401-4867-8b63-ea76a6a917c8";
    try {

        JSONObject resultJson1 = Baseinfo( token, keyword );
        String reason1 = resultJson1.getString( "reason" );//错误信息
        String errorcode1 = resultJson1.getString( "error_code" );//状态码
        if ("ok".equals( reason1 ) && "0".equals( errorcode1 )) {
            JSONObject jsonData = JSONObject.parseObject( resultJson1.getString( "result" ) );
            data.put( "orgNumber", Util.null2String( jsonData.getString( "orgNumber" ) ) );//客户名称
            data.put( "name", Util.null2String( jsonData.getString( "name" ) ) );//客户名称
            data.put( "alias", Util.null2String( jsonData.getString( "alias" ) ) );//客户简称
            data.put( "property3", Util.null2String( jsonData.getString( "property3" ) ) );//英文名称
            data.put( "creditCode", Util.null2String( jsonData.getString( "creditCode" ) ) );//社会统一信用代码
            data.put( "legalPersonName", Util.null2String( jsonData.getString( "legalPersonName" ) ) );//法人代表
            data.put( "regNumber", Util.null2String( jsonData.getString( "regNumber" ) ) );//账号
            data.put( "taxNumber", Util.null2String( jsonData.getString( "taxNumber" ) ) );//税号
        }


        JSONObject resultJson2 = Contact( token, keyword );
        String reason2 = resultJson2.getString( "reason" );//错误信息
        String errorcode2 = resultJson2.getString( "error_code" );//状态码
        if ("ok".equals( reason2 ) && "0".equals( errorcode2 )) {
            JSONObject jsonData = JSONObject.parseObject( resultJson2.getString( "result" ) );
            data.put( "regLocation", Util.null2String( jsonData.getString( "regLocation" ) ) );//地区
            data.put( "phoneNumber", Util.null2String( jsonData.getString( "phoneNumber" ) ) );//电话
            data.put( "email", Util.null2String( jsonData.getString( "email" ) ) );//email
        }


        JSONObject resultJson3 = Annualreport( token, keyword );
        String reason3 = resultJson3.getString( "reason" );//错误信息
        String errorcode3 = resultJson3.getString( "error_code" );//状态码
        if ("ok".equals( reason3 ) && "0".equals( errorcode3 )) {
            JSONObject jsonData = JSONObject.parseObject( resultJson3.getString( "result" ) );
            data.put( "postcode", Util.null2String( jsonData.getString( "postcode" ) ) );//邮政编码
        }


        JSONObject resultJson4 = CreditRating( token, keyword );
        String reason4 = resultJson4.getString( "reason" );//错误信息
        String errorcode4 = resultJson4.getString( "error_code" );//状态码
        if ("ok".equals( reason4 ) && "0".equals( errorcode4 )) {
            JSONObject jsonData = JSONObject.parseObject( resultJson4.getString( "result" ) );
            data.put( "subjectLevel", Util.null2String( jsonData.getString( "subjectLevel" ) ) );//信用等级
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    out.print( data.toJSONString() );
%>
<%!
    public static JSONObject Invoke(String deal, String token, String keyword,Map<String, String> map ) {
        BaseBean baseBean = new BaseBean();
        // get请求返回结果
        JSONObject jsonResult = null;
        CloseableHttpClient client = HttpClients.createDefault();
        // 发送get请求
        String URL = map.get(deal)+"?keyword=" + keyword;;
        HttpGet request = new HttpGet( URL );
        // 设置请求和传输超时时间
        RequestConfig requestConfig = RequestConfig.custom()
                .setSocketTimeout( 6000 ).setConnectTimeout( 6000 ).build();
        request.setConfig( requestConfig );
        request.setHeader( "Authorization", token );//token值
        try {
            CloseableHttpResponse response = client.execute( request );
            //请求发送成功，并得到响应
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                //读取服务器返回过来的json字符串数据
                HttpEntity entity = response.getEntity();
                String strResult = EntityUtils.toString( entity, "UTF-8" );
                //把json字符串转换成json对象
                jsonResult = JSONObject.parseObject( strResult );
            } else {
                baseBean.writeLog( "get请求提交失败:" + URL );
            }
        } catch (IOException e) {
            baseBean.writeLog( "get请求提交失败:" + e );
        } finally {
            request.releaseConnection();
        }
        return jsonResult;
    }
    // 企业基本信息
    public static JSONObject Baseinfo(String token, String keyword) {
        Map<String, String> map = new HashMap();
        map.put("Baseinfo", "http://open.api.tianyancha.com/services/open/ic/baseinfo/normal");
        map.put("Contact", "http://open.api.tianyancha.com/services/open/ic/contact");
        map.put("Annualreport", "http://open.api.tianyancha.com/services/open/ic/annualreport/2.0");
        map.put("CreditRating", "http://open.api.tianyancha.com/services/open/m/creditRating/2.0");
        return Invoke("Baseinfo", token, keyword,map);
    }
    // 企业联系方式
    public static JSONObject Contact(String token, String keyword) {
        Map<String, String> map = new HashMap();
        map.put("Baseinfo", "http://open.api.tianyancha.com/services/open/ic/baseinfo/normal");
        map.put("Contact", "http://open.api.tianyancha.com/services/open/ic/contact");
        map.put("Annualreport", "http://open.api.tianyancha.com/services/open/ic/annualreport/2.0");
        map.put("CreditRating", "http://open.api.tianyancha.com/services/open/m/creditRating/2.0");
        return Invoke("Contact", token, keyword,map);
    }
    // 企业年报
    public static JSONObject Annualreport(String token, String keyword) {
        Map<String, String> map = new HashMap();
        map.put("Baseinfo", "http://open.api.tianyancha.com/services/open/ic/baseinfo/normal");
        map.put("Contact", "http://open.api.tianyancha.com/services/open/ic/contact");
        map.put("Annualreport", "http://open.api.tianyancha.com/services/open/ic/annualreport/2.0");
        map.put("CreditRating", "http://open.api.tianyancha.com/services/open/m/creditRating/2.0");
        return Invoke("Annualreport", token, keyword,map);
    }
    // 企业信用评级
    public static JSONObject CreditRating(String token, String keyword) {
        Map<String, String> map = new HashMap();
        map.put("Baseinfo", "http://open.api.tianyancha.com/services/open/ic/baseinfo/normal");
        map.put("Contact", "http://open.api.tianyancha.com/services/open/ic/contact");
        map.put("Annualreport", "http://open.api.tianyancha.com/services/open/ic/annualreport/2.0");
        map.put("CreditRating", "http://open.api.tianyancha.com/services/open/m/creditRating/2.0");
        return Invoke("CreditRating", token, keyword,map);
    }
%>